package framework.GUI;

import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JFrame;

import framework.automaton.Logic;
import framework.automaton.Mapping;
import framework.automaton.Updater;
import framework.grid.Grid;

/**
 * Class defines the container for which the GUI will be defined. This particular class
 * acts like the controller of the program. It connects the data from the model with the
 * GUI portions of the program such as the GridPanel and the ControlPanel
 * @author Bill Ge, May Zhai, Alex Yoo
 *
 * 
 */
public class MainFrame extends JFrame{
	/** constant for the width of the frame*/
	public static final int FRAME_WIDTH = 650;
	
	public static final int FRAME_BORDER = 25;
	/** constant for the height of the frame*/
	public static final int FRAME_HEIGHT= 700;

	/** reference to the ControlPanel*/
	protected static ControlPanel controls = null;
	/** reference to the GridPanel*/
	protected static GridPanel grid = null;
	/** reference to the model of the program: Updater*/
	protected static Updater updater = null;
	
	/**
	 * create the GUI from logic and a starting Grid
	 * @param logic
	 * @param startGrid
	 */
	public MainFrame(Logic logic, Grid startGrid, Mapping mapping){
		controls = new ControlPanel();
		grid = new GridPanel(startGrid,mapping);
		
		if(logic==null){
			System.out.println("logic in MainFrame must not be null");
			System.exit(0);
		}
		updater = new Updater(logic, startGrid);
		setLayout();
		addPanels();
		init();
	}
	/**
	 * method to set the layout of the panel
	 */
	protected void setLayout(){
		BoxLayout layout = new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS);
		this.setLayout(layout);
	}
	/**
	 * the user is able to create the GUI
	 * @param updater: the model of our program
	 */
	public MainFrame(Updater updater, Mapping mapping){
		controls = new ControlPanel();
		grid = new GridPanel(updater.getGrid(),mapping);
		
		this.updater = updater;
		setLayout();
		addPanels();
		init();
	}

	/**
	 * Adds the ControlPanel and the GridPanel to our frame
	 */
	protected void addPanels(){
		this.getContentPane().add(grid);
		this.getContentPane().add(controls);
	}
	/**
	 * method sets up the main frame to be displayed
	 */
	public void init(){
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
		this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		this.setPreferredSize(new Dimension(FRAME_WIDTH,FRAME_HEIGHT));
		this.setResizable(false);
		
		this.setVisible(true);		
	}
	/**
	 * Method causes the GUI to advance the model by a turn
	 */
	public static void stepForward(){
		Grid temp = updater.getNextTurn();
		grid.updateGrid(temp);
	}
	/**
	 * Method causes the GUI to move the model back a turn
	 */
	public static void stepBackward(){
		Grid temp = updater.getPreviousTurn();
		grid.updateGrid(temp);
	}
}
